﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace EscapeFromLenovo
{
    /// <summary>
    /// Enemy sprites.
    /// </summary>
    class AutoSprite
    {
        //BASIC MEMBER VARIABLES
        Texture2D textureImage;
        public Vector2 position;
        protected Point frameSize;
        int collisionOffset;
        Point currentFrame;
        Point sheetSize;
        int timeSinceLastFrame = 0;
        int millisecondsPerFrame;
        protected Vector2 speed;
        const int defaultMillisecondsPerFrame = 16;
        protected Vector2 startingPosition = Vector2.Zero;
        public float scale = 1;

        //SCORING VARIABLES
        public int scoreValue;

        //CONSTRUCTOR ONE: USES DEFAULT MILLISECONDSPERFRAME
        public AutoSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, int scoreValue, Vector2 speed, float scale) :
            this(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, scoreValue, speed, defaultMillisecondsPerFrame, scale)
        { }

        //CONSTRUCTOR TWO: REQUIRES MILLISECONDSPERFRAME
        public AutoSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, int scoreValue, Vector2 speed, int millisecondsPerFrame, float scale)
        {
            this.textureImage = textureImage;
            this.position = position;
            this.frameSize = frameSize;
            this.collisionOffset = collisionOffset;
            this.currentFrame = currentFrame;
            this.sheetSize = sheetSize;
            this.scoreValue =  scoreValue;
            this.speed = speed;
            this.millisecondsPerFrame = millisecondsPerFrame;
            this.scale = scale;
        }

        //COLLSION RECTANGLE
        public Rectangle collisionRect
        {
            get
            {
                return new Rectangle((int)position.X + collisionOffset,
                                     (int)position.Y + collisionOffset,
                                     frameSize.X - (collisionOffset * 2),
                                     frameSize.Y - (collisionOffset * 2));
            }
        }

        //ENEMY MOVE
        public void Move(GameTime gameTime, Rectangle clientBounds)
        {
            this.position.X += this.speed.X;
            if (position.X > 1180 - textureImage.Width || position.X < 0)
                speed.X *= -1;
        }

        //UPDATE METHOD: for constant animation
        public void Update(GameTime gameTime, Rectangle clientBounds)
        {
            this.Move(gameTime, clientBounds);
            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame > millisecondsPerFrame)
            {
                timeSinceLastFrame = 0;
                ++currentFrame.X;
                if (currentFrame.X >= sheetSize.X)
                {
                    currentFrame.X = 0;
                    ++currentFrame.Y;
                    if (currentFrame.Y >= sheetSize.Y)
                        currentFrame.Y = 0;
                }
            }
        }

        //UPDATE METHOD: for life images
        public void lifeUpdate()
        {
            currentFrame.X = 0;
            currentFrame.Y = 0;
        }

        //DRAW METHOD
        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(textureImage, position, new Rectangle(currentFrame.X * frameSize.X,
                                                                   currentFrame.Y * frameSize.Y,
                                                                   frameSize.X, frameSize.Y), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
        }
    }
}